package com.multi.thread.chapter04.demo11;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 *	tryLock(Long time, TimeUnit unit):如果锁定在给定等待时间内没有被另一个线程保持，且当前线程未被中断，在获取该锁定
 */
public class TestTryLockLimit {
	
	private ReentrantLock lock = new ReentrantLock();
	
	public void waitMethod() {
		try {
			if (lock.tryLock(3, TimeUnit.SECONDS)) {
				System.out.println(Thread.currentThread().getName() + " 获得锁的时间：" + System.currentTimeMillis());
				Thread.sleep(10000);
			} else {
				System.out.println(Thread.currentThread().getName() + " 没有获得锁!");
			}
		} catch (InterruptedException e) {
			e.printStackTrace();
		} finally {
			if (lock.isHeldByCurrentThread())
				lock.unlock();
		}
	}
	
	public static void main(String[] args) throws InterruptedException {
		final TestTryLockLimit service = new TestTryLockLimit();
		Runnable run = new Runnable() {
			@Override
			public void run() {
				System.out.println(Thread.currentThread().getName() + " 调用waitMethod时间：" + System.currentTimeMillis());
				service.waitMethod();
			}
		};
		Thread a = new Thread(run);
		a.setName("A");
		a.start();
		Thread b = new Thread(run);
		b.setName("B");
		b.start();
	}

}
